package org.dromara.oce.service;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.oce.config.DsdjConfig;
import org.dromara.oce.domain.bo.IaaBackBo;
import org.dromara.oce.domain.dto.TradeOrderIaaBackRecordDTO;
import org.dromara.oce.domain.dto.TradeOrderIaaBackRecordExcelDTO;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * iaa回传Service业务层处理
 *
 * @author Lion Li
 * @date 2024-12-24
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class OceIaaBackService {

    private final DsdjConfig dsdjConfig;

    /**
     * 分页查询iaa回传记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return iaa回传记录分页列表
     */
    public TableDataInfo<TradeOrderIaaBackRecordDTO> queryPageList(IaaBackBo bo, PageQuery pageQuery) {
        TableDataInfo<TradeOrderIaaBackRecordDTO> tableDataInfo = new TableDataInfo<>();
        List<TradeOrderIaaBackRecordDTO> dataList = new ArrayList<>();
        long total = 0L;
        Map<String, Object> params = new HashMap<>();
        params.put("referralLinkId", bo.getReferralLinkId());
        params.put("backFlag", bo.getBackFlag());
        params.put("promotionId", bo.getPromotionId());
        params.put("startDate", StringUtils.isBlank(bo.getStartDate()) ? "" : bo.getStartDate().length() > 10 ? bo.getStartDate() : bo.getStartDate() + " 00:00:00");
        params.put("endDate", StringUtils.isBlank(bo.getEndDate()) ? "" : bo.getEndDate().length() > 10 ? bo.getEndDate() : bo.getEndDate() + " 23:59:59");
        params.put("userId", bo.getUserId());
        params.put("current", pageQuery.getPageNum());
        params.put("size", pageQuery.getPageSize());
        params.put("column", pageQuery.getOrderByColumn());
        params.put("asc", StringUtils.equals("asc", pageQuery.getIsAsc()));
        String url = dsdjConfig.getDsdjServerReqeustUrl() + "/system/inner/getIaaBackRecordPage";
        String result = HttpUtil.get(url, params);
        JSONObject jsonObject = new JSONObject(result);
        int code = jsonObject.getInt("code", 0);
        String msg = jsonObject.getStr("msg");
        if (code == 200) {
            JSONObject data = jsonObject.getJSONObject("data");
            total = data.getLong("total");
            dataList = data.getBeanList("records", TradeOrderIaaBackRecordDTO.class);
        }
        tableDataInfo.setRows(dataList);
        tableDataInfo.setTotal(total);
        tableDataInfo.setCode(code);
        tableDataInfo.setMsg(msg);
        return tableDataInfo;
    }

    /**
     * 查询iaa回传记录列表
     *
     * @param bo 查询条件
     * @return iaa回传记录列表
     */
    public List<TradeOrderIaaBackRecordExcelDTO> queryList(IaaBackBo bo) {
        List<TradeOrderIaaBackRecordExcelDTO> dataList = new ArrayList<>();
        Map<String, Object> params = new HashMap<>();
        params.put("referralLinkId", bo.getReferralLinkId());
        params.put("backFlag", bo.getBackFlag());
        params.put("promotionId", bo.getPromotionId());
        params.put("startDate", StringUtils.isBlank(bo.getStartDate()) ? "" : bo.getStartDate().length() > 10 ? bo.getStartDate() : bo.getStartDate() + " 00:00:00");
        params.put("endDate", StringUtils.isBlank(bo.getEndDate()) ? "" : bo.getEndDate().length() > 10 ? bo.getEndDate() : bo.getEndDate() + " 23:59:59");
        params.put("userId", bo.getUserId());
        try {
            String url = dsdjConfig.getDsdjServerReqeustUrl() + "/system/inner/getIaaExcel";
            String result = HttpUtil.get(url, params);
            JSONObject jsonObject = new JSONObject(result);
            int code = jsonObject.getInt("code", 0);
            if (code == 200) {
                dataList = jsonObject.getBeanList("data", TradeOrderIaaBackRecordExcelDTO.class);
                if(CollectionUtils.isNotEmpty(dataList)){
                    for (TradeOrderIaaBackRecordExcelDTO data : dataList) {
                        data.setReportBackStatusStr(data.getReportBackStatus());
                        data.setBackFlagStr(data.getBackFlag());
                        data.setReferralLinkIdStr(data.getReferralLinkId());
                    }
                }
            }
        } catch (Exception e) {
        }
        return dataList;
    }

    /**
     * iaa手动上报
     *
     * @param id 回传记录id
     * @return iaa手动上报结果
     */
    public Boolean reportIaaBack(Long id) {
        boolean flag = false;
        Map<String, Object> params = new HashMap<>();
        params.put("iaaEcpmBackId", id);
        String url = dsdjConfig.getDsdjServerReqeustUrl() + "/system/inner/reportIaaBack";
        String result = HttpUtil.get(url, params);
        JSONObject jsonObject = new JSONObject(result);
        int code = jsonObject.getInt("code", 0);
        if (code == 200) {
            flag = jsonObject.getBool("data", true);
        }
        return flag;
    }

}
